using UnityEngine;

public class ButtonPositionSaver : UIDragDropItem
{
	public string keyButton;

	public float xpos;

	public float ypos;

	public float scale;

	public static string key = "KEY_SAVE_";

	[HideInInspector]
	public ControlsPositionsSaver master;

	public bool right;

	public UIWidget parentWidget;

	public UISprite sprite;

	public UIWidget leftAnchor;

	public UIWidget rightAnchor;

	private bool isLeftOrientation;

	protected override void Awake()
	{
		base.Awake();
		xpos = base.transform.localPosition.x;
		ypos = base.transform.localPosition.y;
		scale = GetScale();
	}

	protected override void OnEnable()
	{
		base.OnEnable();
		CancelSettings();
	}

	protected override void OnDisable()
	{
		SaveSettings();
	}

	protected override void OnDragDropMove(Vector2 delta)
	{
		if (!(parentWidget == null))
		{
			Vector3 pos = base.transform.localPosition + (Vector3)delta;
			base.transform.localPosition = ClampPosition(pos);
			master.SetCurrentButton(this);
		}
	}

	protected override void OnPress(bool isPressed)
	{
		base.OnPress(isPressed);
		if (isPressed)
		{
			master.SetCurrentButton(this);
		}
	}

	public void DefaultSettings()
	{
		SetOrientation(false);
		base.transform.localPosition = new Vector3(xpos, ypos, base.transform.localPosition.z);
		SetScale(scale);
	}

	public void CancelSettings()
	{
		if (PlayerPrefs.HasKey(key + keyButton))
		{
			LoadSettings();
		}
		else
		{
			DefaultSettings();
		}
	}

	public void SaveSettings()
	{
		ButtonPositionInfo buttonPositionInfo = new ButtonPositionInfo(base.transform.localPosition, GetScale());
		PlayerPrefs.SetString(key + keyButton, buttonPositionInfo.ToString());
	}

	private void LoadSettings()
	{
		ButtonPositionInfo buttonPositionInfo = new ButtonPositionInfo(PlayerPrefs.GetString(key + keyButton));
		bool flag = (!isLeftOrientation && right) || (!right && isLeftOrientation);
		base.transform.localPosition = new Vector3(buttonPositionInfo.param[0] * ((!flag) ? 1f : (-1f)), buttonPositionInfo.param[1], 0f);
		SetScale(buttonPositionInfo.param[2]);
	}

	public float GetScale()
	{
		return base.transform.localScale.x - 0.5f;
	}

	public void SetScale(float value)
	{
		base.transform.localScale = new Vector3(0.5f + value, 0.5f + value, 1f);
	}

	private Vector3 ClampPosition(Vector3 pos)
	{
		if ((isLeftOrientation && right) || (!right && !isLeftOrientation))
		{
			pos.x = Mathf.Clamp(pos.x, sprite.width / 2, parentWidget.width - sprite.width / 2);
		}
		else
		{
			pos.x = Mathf.Clamp(pos.x, -parentWidget.width + sprite.width / 2, -sprite.width / 2);
		}
		pos.y = Mathf.Clamp(pos.y, sprite.height / 2, parentWidget.height);
		return pos;
	}

	public void SetOrientation(bool value)
	{
		if (isLeftOrientation != value && parentWidget != null)
		{
			isLeftOrientation = value;
			Vector3 localPosition = base.transform.localPosition;
			if ((isLeftOrientation && right) || (!right && !isLeftOrientation))
			{
				base.transform.SetParent(leftAnchor.transform);
			}
			else
			{
				base.transform.SetParent(rightAnchor.transform);
			}
			base.transform.localPosition = new Vector3(0f - localPosition.x, localPosition.y, localPosition.z);
		}
	}

	public void SetColor(Color color)
	{
		sprite.color = color;
	}
}
